package com.example.xutilsdemo.db

import com.blankj.utilcode.util.LogUtils
import com.example.xutilsdemo.app.MyApp
import com.example.xutilsdemo.utils.FireGsonUtil
import org.xutils.DbManager
import org.xutils.common.util.KeyValue
import org.xutils.db.sqlite.WhereBuilder
import org.xutils.ex.DbException
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.PrintStream

/**
 *@author: njb
 *@date:  2023/5/24 23:04
 *@desc:
 */
class UserInfoDBManager {
    private val TAG = "UserInfoDbManager"

    companion object {
        private lateinit var mDbManager: DbManager
        val instance by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { UserInfoDBManager() }
    }

    fun initDB() {
        mDbManager = MyApp.instance.getLocalDbManager()
    }

    /**
     * 保存用户信息
     *
     * @param UserInfoDataBaseBean 传入用户信息
     */
    open fun saveUserInfo(UserInfoDataBaseBean: UserInfoDataBaseBean) {
        try {
            LogUtils.i(TAG, "=====saveUserInfo: ${FireGsonUtil.objectToJson(UserInfoDataBaseBean)}")
            mDbManager.saveOrUpdate(UserInfoDataBaseBean)
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "====saveUserInfo: 出错！！ " + getExceptionInfo(e))
        }
    }


    /**
     * 通过name删除用户信息
     *
     * @param name return 删除条数 -1则异常
     */
    fun deleteByDeviceName(name: String?): Int {
        return try {
            mDbManager.delete(
                UserInfoDataBaseBean::class.java,
                WhereBuilder.b().and("name", "=", name)
            )
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "deleteByName: " + getExceptionInfo(e))
            -1
        }
    }

    /**
     * 通过id删除用户
     *
     * @param id
     * @return 删除条数 -1则异常
     */
    fun deleteByUserId(id: Int): Int {
        return try {
            mDbManager.delete(
                UserInfoDataBaseBean::class.java,
                WhereBuilder.b().and("id", "=", id)
            )
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "deleteByRunName: " + getExceptionInfo(e))
            -1
        }
    }


    /**
     * 删除所有用户信息-删除表
     */
    fun deleteAllUsers() {
        try {
            mDbManager.delete(UserInfoDataBaseBean::class.java)
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "deleteAllUsers: " + getExceptionInfo(e))
        }
    }

    fun isUserInfoExist(): Any {
        val any = try {
            mDbManager.getTable(UserInfoDataBaseBean::class.java).tableIsExists()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return any
    }


    /**
     * 更新用户信息状态
     *
     * @param name
     * @return 修改多少条数
     */
    fun updateByName(name: String?, vararg kv: KeyValue?): Int {
        return try {
            mDbManager.update(
                UserInfoDataBaseBean::class.java,
                WhereBuilder.b().and("name", "=", name),
                *kv
            )
        } catch (e: Exception) {
            e.printStackTrace()
            LogUtils.e(TAG, "updateByName: " + getExceptionInfo(e))
            -1
        }
    }

    /**
     * Id更新用户状态
     *
     * @param id
     * @return 修改多少条数
     */
    fun updateById(id: String?, vararg kv: KeyValue?): Int {
        return try {
            mDbManager.update(
                UserInfoDataBaseBean::class.java,
                WhereBuilder.b().and("id", "=", id),
                *kv
            )
        } catch (e: Exception) {
            e.printStackTrace()
            LogUtils.e(TAG, "updateById: " + getExceptionInfo(e))
            -1
        }
    }

    /**
     * 更新用户状态
     *
     * @param UserInfoDataBaseBean 传入结构体更新数据库
     */
    fun updateUserInfo(UserInfoDataBaseBean: UserInfoDataBaseBean?) {
        try {
            mDbManager.replace(UserInfoDataBaseBean)
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "updateUserInfo: " + getExceptionInfo(e))
        }
    }


    /**
     * 获取所有用户
     *
     * @return List<UserInfoDataBaseBean>
     **/
    fun getAllUserInfo(): List<UserInfoDataBaseBean>? {
        return try {
            mDbManager.findAll(UserInfoDataBaseBean::class.java)
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "getAllUserInfo: " + getExceptionInfo(e))
            null
        }
    }

    /**
     * 根据用户名称查找用户
     *
     * @param name
     * @return UserInfoDataBaseBean
     */
    fun queryUserInfoByName(name: String): UserInfoDataBaseBean? {
        return try {
            mDbManager.selector(UserInfoDataBaseBean::class.java).where("name", "=", name)
                .findFirst()
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "queryUserInfoByName: " + getExceptionInfo(e))
            null
        }
    }

    /**
     *
     *
     * @param
     */
    fun queryListUserInfoByName(name: String): List<UserInfoDataBaseBean?>? {
        return try {
            mDbManager.selector(
                UserInfoDataBaseBean::class.java
            ).where("name", "=", name).findAll()
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "queryListUserInfoByName: " + getExceptionInfo(e))
            null
        }
    }


    /**
     * 根据Id查找用户信息
     *
     * @param id
     * @return UserInfoDataBaseBean
     */
    fun queryDeviceById(id: String?): UserInfoDataBaseBean? {
        return try {
            mDbManager.selector(UserInfoDataBaseBean::class.java).where("id", "=", id)
                .findFirst()
        } catch (e: DbException) {
            e.printStackTrace()
            LogUtils.e(TAG, "queryDeviceById: " + getExceptionInfo(e))
            null
        }
    }

    private fun getExceptionInfo(e: Exception): String {
        val byteArray = ByteArrayOutputStream()
        e.printStackTrace(PrintStream(byteArray))
        return byteArray.toString()
    }
}


